package com.eqdd.yiqidian.ui.mine.shenqing.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseShowFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarPaperListBean;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.TiaogangListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.mine.pizhun.TiaogangInfoActivity;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lvzhihao on 17-7-19.
 */

public class MyCreateTiaogangFragment extends BaseShowFragment {
    private SlimAdapterEx<TiaogangListBean> slimAdapterEx;
    private ModelRx2DataSource<TiaogangListBean> dataSource;
    private MVCCoolHelper<List<TiaogangListBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    protected void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_28, new SlimInjector<TiaogangListBean>() {
                @Override
                public void onInject(TiaogangListBean data, IViewInjector injector) {
                    injector.text(R.id.tv_left_content, data.getChangeDepartment())
                            .text(R.id.tv_right_hint_content, data.getChangePost());
                }
            }).attachTo(dataBinding.recyclerView);
            dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
                Bundle bundle = new Bundle();
                bundle.putInt(Config.ID, slimAdapterEx.getData().get(position).getId());
                bundle.putInt(Config.STATUS, status);
                bundle.putInt(Config.WHO, who);
                startActivity(TiaogangInfoActivity.class, bundle);
            });
            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;
                    String url = "";
                    if (who == 0) {
                        url = HttpConfig.MY_TIAOGANG_CREATE_LIST;
                    } else if (who == 1) {
                        url = HttpConfig.TIAOGANG_CHECK_LIST;
                    }
                    return OkGo.<HttpResultRows<TiaogangListBean>>post(HttpConfig.BASE_URL + url)
                            .params("page", pageNum)
                            .params("companyId", GreenDaoUtil.getUser().getCompanyId())
                            .params("userGuid", GreenDaoUtil.getUser().getGuid())
                            .params("type", status)
                            .converter(new JsonConverter<HttpResultRows<TiaogangListBean>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(httpResult -> {
                                pageNum = httpResult.getItems().getPage();
                                return Flowable.just(httpResult.getItems().getRows() == null ? new ArrayList<TiaogangListBean>() : httpResult.getItems().getRows());
                            });

                }
            }, 10);
            slimAdapterEx.updateData(new ArrayList<>());
            mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void refresh() {
        mvcHelper.refresh();
    }
}
